<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/22
//      |_|                                         | TpMeCMS

declare(strict_types=1);
namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 统计
 * @Auth(name="token",nologin={"no"})
 * @Controller("/user.bill/")
 */
class Bill extends UserBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];





    /**
     * 获取统计邀请
     * @Action
     * @return void
     */
    public function get_count()
    {
        $where['user_id'] = $user_id = $this->auth->user()->id;

        $我邀请的会员数 = Db::name('user')->where('pid', '=', $user_id)->count();
        $invite_user_ids = Db::name('user')->where('pid', '=', $user_id)->column('id');
        $store_num = Db::name('store')->where('user_id', 'in', $invite_user_ids)->count();
        $辅导员店铺 = Db::name('store')->where('pid', '=', $user_id)->count();
        $间接会员数 = 0;
        if ($invite_user_ids) {
            foreach ($invite_user_ids as $key => $id) {
                $temp = Db::name('user')->where('pid', 'in', $invite_user_ids)->count();
                $间接会员数 = $间接会员数 + $temp ? $temp : 0;
            }
        }


        $我邀请的会员数 = $我邀请的会员数 ? $我邀请的会员数 : 0;
        $间接会员数 = $间接会员数 ? $间接会员数 : 0;
        $store_num = $store_num ? $store_num : 0;
        $辅导员店铺 =  $辅导员店铺 ? $辅导员店铺 : 0;
        $data['user_num'] = $我邀请的会员数;
        $data['store_num'] = $store_num;
        $data['indirect_user_num'] = $间接会员数;
        $data['coach_store_num'] =  $辅导员店铺;
        // $data['out'] = Db::name('signlog')->whereEx($where)->where('status','1')->sum('signlog');  
        // $data['this_month_inc'] = Db::name('signlog')->whereEx($where)->whereTime('createtime', 'm')->where('status','2')->sum('signlog');  
        // $data['this_month_dec'] = Db::name('signlog')->whereEx($where)->whereTime('createtime', 'm')->where('status','1')->sum('signlog');  
        return $this->success('统计', $data);
    }



    
    /**
     * 获取邀请会员列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $search 搜索
     * @return void
     * @Action
     */
    public function get_invite_user_list(?int $page = 1, ?int  $limit = 10, ?string $search = null)
    {
        $page = $page ? $page : 1;
        $limit = $limit ? $limit : 10;

        $data['pid'] = $this->auth->user()->id;
        if ($search) {
            $data['nickname'] = ['like', "%" . $search . "%"];
        }

        $list = Db::name('user')->orderRaw('id desc')->whereEx($data)->fieldRaw('nickname,avatar,mobile,id,createtime,set_id')->page($page, $limit)->select()->getArray();

        if ($list)
            foreach ($list as $key => &$val) {
                $val['mobile'] = $this->HideMobile($val['mobile']);
                // $this->ChangeFieldData($val);
                $val['date'] = date('Y-m-d H:i', $val['createtime']);
                $val['f'] = 0;
                $val['set_name'] = Db::name('set')->where('id', '=', $val['set_id'])->value('name');
            }
        return $this->success('ok', $list);
    }

    /**
     * 获取邀请店铺列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $search 搜索
     * @return void
     * @Action
     */
    public function get_invite_store_list(?int $page = 1, ?int  $limit = 10, ?string $search = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;


        $invite_user_ids = Db::name('user')->where('pid', '=', $this->auth->user()->id)->column('id');
        if ($search) {
            $data['name'] = ['like', "%" . $search . "%"];
        }
        $data['user_id'] = ['in', $invite_user_ids];

        $list = Db::name('store')->whereEx($data)->page($page, $limit)->select()->getArray();
        // ->orderRaw('id desc')->field('nickname,mobile,id,createtime')->page($page, $limit)->select()->getArray();
        if ($list)
            foreach ($list as $key => &$val) {
                // $val['mobile'] = $this->HideMobile($val['mobile']);
                // $this->ChangeFieldData($val);
                $val['date'] = date('Y-m-d H:i', $val['createtime']);
            }
        return $this->success('ok', $list);
    }


    /**
     * 获取辅导员邀请店铺列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $search 搜索
     * @return void
     * @Action
     */
    public function get_coach_store_list(?int $page = 1, ?int  $limit = 10, ?string $search = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;

        if ($search) {
            $data['name'] = ['like', "%" . $search . "%"];
        }
        $data['pid'] = $this->auth->user()->id;

        $list = Db::name('store')->whereEx($data)->page($page, $limit)->select()->getArray();

        if ($list)
            foreach ($list as $key => &$val) {
                // $val['mobile'] = $this->HideMobile($val['mobile']);
                // $this->ChangeFieldData($val);
                $val['date'] = date('Y-m-d H:i', $val['createtime']);
            }
        return $this->success('ok', $list);
    }
}
